package com.sh.service.impl;

import com.sh.bean.CartItem;
import com.sh.bean.Resorder;
import com.sh.bean.Resorderitem;
import com.sh.bean.Resuser;
import com.sh.dao.ResorderMapper;
import com.sh.dao.ResorderitemMapper;
import com.sh.service.ResorderBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;


@Service
@Transactional(
        propagation = Propagation.REQUIRED,
        isolation = Isolation.DEFAULT,
        timeout = 2000,
        readOnly = false,
        rollbackFor = {Exception.class, RuntimeException.class}
)
public class ResorderBizImpl implements ResorderBiz {

    @Autowired
    private ResorderMapper resorderMapper;
    @Autowired
    private ResorderitemMapper resorderitemMapper;

    @Override
    public int order(Resorder resorder, Set<CartItem> cartItems, Resuser resuser) {
        resorder.setUserid(resorder.getUserid());  // 用户id
        this.resorderMapper.insert(resorder);
        for (CartItem ci : cartItems) {
            Resorderitem roi = new Resorderitem();
            roi.setRoid(resorder.getRoid());
            roi.setFid( ci.getFood().getFid() );
            roi.setDealprice( ci.getFood().getRealprice() );
            roi.setNum(ci.getNum() );
            this.resorderitemMapper.insert(roi);
        }
        return 1;
    }
}
